package com.community.process._1010.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.process._1010.common.api.ErrorAssert;
import com.community.process._1010.common.utils.PageUtil;
import com.community.process._1010.common.utils.mp.MpPageFormat;
import com.community.process._1010.dao.entity.ProcessFoodItemIngredients;
import com.community.process._1010.dao.mapper.ProcessFoodItemIngredientsMapper;
import com.community.process._1010.dao.vo.ProcessFoodItemIngredientsVo;
import com.community.process._1010.dao.vo.ProcessFoodItemVo;
import com.community.process._1010.domains.ProcessFoodItemIngredientsDTO;
import com.community.process._1010.service.ProcessFoodItemIngredientsService;
import com.oneadds.lowcode.BaseServiceImpl;
import com.oneadds.lowcode.dto.PageListData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: Administrator to Zbs
 * @Date: 2024/9/6 星期五 13:09
 */
@Service
@RequiredArgsConstructor
public class ProcessFoodItemIngredientsServiceImpl extends BaseServiceImpl<ProcessFoodItemIngredientsMapper, ProcessFoodItemIngredients> implements ProcessFoodItemIngredientsService {

    @Override
    public PageListData<ProcessFoodItemIngredientsVo> getProcessFoodItemIngredientsVoPageList(ProcessFoodItemIngredientsDTO.FoodItemIngredientsPageSearch dto) {
        // yg_process_food_item_ingredients ypfii 成品菜食材列表
        // yg_process_ingredients ypi 食材
        ErrorAssert.notNull(dto.getFoodItemId(), "请选择菜品");
        QueryWrapper<ProcessFoodItemIngredientsVo> wrapper = new QueryWrapper<>();
        wrapper.eq("ypfii.food_item_id", dto.getFoodItemId());
        wrapper.like(StrUtil.isNotBlank(dto.getName()),"ypfii.`name`", dto.getName());
        wrapper.eq("ypfii.deleted", 0);
        wrapper.eq("ypi.deleted", 0);
        wrapper.orderByDesc("ypfii.`create_time`");
        Page<ProcessFoodItemIngredientsVo> page = this.baseMapper.getProcessFoodItemIngredientsVoPageList(PageUtil.page(dto), wrapper);
        return MpPageFormat.format(page);
    }

    @Override
    public List<ProcessFoodItemIngredientsVo> getProcessFoodItemIngredientsVoList(Long foodItemId) {
        ErrorAssert.notNull(foodItemId, "请选择菜品");
        return this.getProcessFoodItemIngredientsVoListByInFoodItemId(List.of(foodItemId));
    }

    @Override
    public List<Long> getFoodItemIdListByInIngredientsId(List<Long> ingredientsIdList) {
        return this.lambdaQuery()
                .select(ProcessFoodItemIngredients::getFoodItemId)
                .in(ProcessFoodItemIngredients::getIngredientsId, ingredientsIdList)
                .list()
                .stream()
                .map(ProcessFoodItemIngredients::getFoodItemId)
                .collect(Collectors.toList());
    }

    @Override
    public List<ProcessFoodItemIngredientsVo> getProcessFoodItemIngredientsVoListByInFoodItemId(List<Long> foodItemIds) {
        // yg_process_food_item_ingredients ypfii 成品菜食材列表
        // yg_process_ingredients ypi 食材
        if (CollectionUtils.isEmpty(foodItemIds)) {
            return List.of();
        }
        QueryWrapper<ProcessFoodItemIngredientsVo> wrapper = new QueryWrapper<>();
        wrapper.in("ypfii.food_item_id", foodItemIds);
        wrapper.eq("ypfii.deleted", 0);
        wrapper.eq("ypi.deleted", 0);
        wrapper.orderByDesc("ypfii.`create_time`");
        return this.baseMapper.getProcessFoodItemIngredientsVoList(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addFoodItemIngredients(Long foodItemId, List<ProcessFoodItemIngredientsDTO.IngredientsFoodItemAdd> list) {
        List<ProcessFoodItemIngredients> collect = list.stream().map(item -> new ProcessFoodItemIngredients(foodItemId, item.getIngredientsId(), item.getWeight())).collect(Collectors.toList());
        this.saveBatch(collect);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addFoodItemIngredients(ProcessFoodItemIngredientsDTO.FoodItemIngredientsAdd dto) {
        ProcessFoodItemIngredients foodItemIngredients = new ProcessFoodItemIngredients(dto.getFoodItemId(), dto.getIngredientsId(), dto.getWeight());
        this.save(foodItemIngredients);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateFoodItemIngredients(ProcessFoodItemIngredientsDTO.FoodItemIngredientsUpdate dto) {
        ProcessFoodItemIngredients foodItemIngredients = new ProcessFoodItemIngredients();
        foodItemIngredients.setId(dto.getId());
        foodItemIngredients.setIngredientsId(dto.getIngredientsId());
        foodItemIngredients.setWeight(dto.getWeight());
        this.updateById(foodItemIngredients);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteFoodItemIngredients(ProcessFoodItemIngredientsDTO.FoodItemIngredientsDelete dto) {
        this.removeByIds(dto.getIds());
    }

    @Override
    public void deleteFoodItemIngredients(Long foodItemId) {
        this.lambdaUpdate().eq(ProcessFoodItemIngredients::getFoodItemId, foodItemId).remove();
    }
}
